<template>
  <a-card>
    <template #title>
      <a-form>
        <a-row :gutter="16">
          <a-col  :md="6" :xs="24" >
            <a-form-item v-if="isPermission('leader')" :label="$t('客服组长')">
              <a-select v-model:value="page.employeeLeaderId" allowClear   >
                <a-select-option v-for="item in leaderList" :key="item.systemId" :value="item.systemId">
                  {{ item.systemName }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :xs="24" >
            <a-form-item :label="$t('客服')">
              <a-select v-model:value="page.employeeId" allowClear>
                <a-select-option v-for="item in systemUserList" :key="item.id" :value="item.id">{{
                    item.name
                  }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :xs="24">
            <a-form-item :label="$t('员工账号')">
              <a-input v-model:value="page.systemAccount"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="8" :xs="24">
            <a-form-item :label="$t('日期')">
              <a-range-picker v-model:value="page.timeValue" :allowClear="false" format="YYYY-MM-DD"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col style="text-align: right" :span="24">
            <a-button type="primary" @click="getTableData">{{ $t('查询') }}</a-button>
            <a-button style="margin-left: 10px" @click="resetSearch">{{ $t('重置') }}</a-button>
          </a-col>
        </a-row>
      </a-form>
      <div style="margin-top:30px">
        <h3>{{$t('合计')}}</h3>
      </div>
      <a-table style="margin-top: 15px" :pagination="false" :columns="totalTableColumn"  :data-source="totalTableData" :scroll="{x:1000}"
               bordered>
      </a-table>

    </template>
    <a-table  :columns="tableColumn" :pagination="pagination" :loading="loading" :data-source="tableData" :scroll="{x:1000}"
              bordered>
    </a-table>
  </a-card>
</template>

<script>
import {reactive, ref, toRefs, onMounted, computed, } from 'vue'
import {fetchAchievementReport, fetchAchievementReportTotal} from "@/api/report"
import locale from '@/locale'
const {t} = locale.global;
import {dateFormatTimeStamp, defaultTimeDate,hasPermission} from '@/utils'
import {selectSystemUser} from "../../../api";
import {selectLeader} from "../../../api/authority";
const   tableColumn= [
  {title: t('时间'), dataIndex: 'date', width: 150, align: 'center', fixed: 'left',},
  {title: t('员工账号'), dataIndex: 'systemAccount', width: 150, align: 'center', },
  {title: t('员工姓名'), dataIndex: 'systemName', width: 150, align: 'center', },
  {title: t('新增订单数'), dataIndex: 'insertNum', width: 150, align: 'center', },
  {title: t('库存订单数'), dataIndex: 'historyNum', width: 150, align: 'center', },
  {title: t('保证金付款数'), dataIndex: 'loanNum', width: 150, align: 'center', },
  {title: t('解冻费付款数'), dataIndex: 'thawNum', width: 150, align: 'center', },
  {title: t('事故费付款数'), dataIndex: 'accidentNum', width: 150, align: 'center', },
  {title: t('插队费付款数'), dataIndex: 'jumpQueueNum', width: 150, align: 'center', },
  {title: t('付款总金额'), dataIndex: 'paySum', width: 150, align: 'center', },
  {title: t('日目标'), dataIndex: 'target', width: 150, align: 'center', },
  {title: t('日目标达成率'), dataIndex: 'achievingRateStr', width: 150, align: 'center', },
]
const totalTableColumn = [
  {title: t('新增订单数'), dataIndex: 'insertNum', width: 150, align: 'center', },
  {title: t('库存订单数'), dataIndex: 'historyNum', width: 150, align: 'center', },
  {title: t('保证金付款数'), dataIndex: 'loanNum', width: 150, align: 'center', },
  {title: t('解冻费付款数'), dataIndex: 'thawNum', width: 150, align: 'center', },
  {title: t('事故费付款数'), dataIndex: 'accidentNum', width: 150, align: 'center', },
  {title: t('插队费付款数'), dataIndex: 'jumpQueueNum', width: 150, align: 'center', },
  {title: t('付款总金额'), dataIndex: 'paySum', width: 150, align: 'center', },
  {title: t('日目标'), dataIndex: 'target', width: 150, align: 'center', },
  {title: t('日目标达成率'), dataIndex: 'achievingRateStr', width: 150, align: 'center', },
]
export default {
  name: "AchievementReport",
  setup() {
    const formRef = ref(null)
    const state = reactive({
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        timeValue: defaultTimeDate(30)
      },
      tableRows: {},
      incomeDetailsVisible: false,
      tableData: [],
      totalTableData: [],
      loading:false,
      systemUserList: [],
      leaderList:[]
    })
    const pagination = computed(() => ({
      current: state.page.pageNum,
      total: state.page.total,
      pageSize: state.page.pageSize,
      showSizeChanger: true,
      showQuickJumper: true,
      pageSizeOptions: ['10', '50', '100', '200'],
      showTotal: total => total ,
      onChange: (page) => {
        state.page.pageNum = page
        method.getTableData()
      },
      onShowSizeChange: (page, size) => {
        state.page.pageNum = page
        state.page.pageSize = size
        method.getTableData()
      }
    }))
    const method = {
      async getTableData() {
        const [startTime, endTime] = dateFormatTimeStamp(state.page.timeValue || [])
        const params = {
          startTime, endTime,
          ...state.page,
        }
        delete params.timeValue
        state.loading = true
        const res = await fetchAchievementReport(params)
        if (res.code === 1) {
          const {list, total} = res.data
          state.tableData = list
          state.page.total = total
          await  method.getTotal(params)
        }
        state.loading = false
      },
      async getTotal(params) {
        const res = await fetchAchievementReportTotal(params)
        if (res.code === 1) {
          if (state.tableData.length > 0) {
            const totalData = res.data
            state.totalTableData = [
              {...totalData, date: t('合计')},
            ]
          } else {
            state.totalTableData = []
          }
        }
      },
      resetSearch() {
        state.page = {
          pageNum: 1,
          pageSize: 10,
          total: 0
        }
        method.getTableData()
      },
    }
    function isPermission(value) {
      return hasPermission('Index', value)
    }

    onMounted(() => {
      method.getTableData()
      selectSystemUser().then(res => {
        state.systemUserList = res.data
      })
      if (isPermission('leader')) {
        selectLeader().then(res => {
          state.leaderList = res.data
        })
      }
    })
    return {
      tableColumn,
      totalTableData:[],
      totalTableColumn,
      formRef,
      pagination,
      isPermission,
      ...method,
      ...toRefs(state)
    }
  }
}
</script>

<style scoped>

</style>

